﻿var rainIcon = "images/weather/weather_icon_04.png";
var sunIcon  = "images/weather/weather_icon_12.png";
var cloudSunIcon = "images/weather/weather_icon_07.png";
var snowIcon = "images/weather/weather_icon_10.png";

var xAxis;
var skinTemp;
var soilTemp;
var wind;
var rain;
var snow;
var visibility;
var hum;
var cloud;

function calcData(locID,limit, location_name){
	locID = locID == null ? 1 : locID;
	xAxis = new Array;
	skinTemp = new Array;
	soilTemp = new Array;
	wind = new Array;
	rain = new Array;
	snow = new Array;
	visibility = new Array;
	hum = new Array;
	cloud = new Array;
	$.ajax({
		type : 'POST',
		dataType : 'json',
		data : {
			'loc':locID, 
			'limit':limit
		},
		url : 'index.php?r=data/getChartData',
		success : function(data) {
				var i = 0;
		var lastIcon = 7;
		$.each(data, function(key, val) {		
			xAxis.push(val.datetime_stamp);
			//skinTemp.push(Math.round((parseFloat(val.skin_temp)-273.15)*10)/10);
			soilTemp.push(Math.round((parseFloat(val.soil_temp)-273.15)*10)/10);
			visibility.push(Math.round(parseInt(val.visibility)/10)/100);
			rain.push(Math.round(parseFloat(val.total_col_rain)*100)/100);
			snow.push(Math.round(parseFloat(val.total_col_snow)*100)/100);
			hum.push(Math.round(parseFloat(val.surf_rel_hum)*100)/100);
			cloud.push(Math.round(((parseFloat(val.low_cloud_fra)+parseFloat(val.mid_cloud_fra)+parseFloat(val.high_cloud_fra))/3)*100)/100);
			
			var u = parseFloat(val.u_wind_10m);
			var v = parseFloat(val.v_wind_10m);
			var vWind = Math.round(Math.sqrt(u*u + v*v)*100)/100;
			
			var deg = Math.acos(u/vWind)*(180/Math.PI);
			if(v >= 0) deg = -deg;
		
			var icon = sunIcon;
			if (snow[snow.length-1]  > 1) 
				icon = snowIcon;
			else if (rain[rain.length-1] > 1) 
				icon = rainIcon;
			else if (cloud[cloud.length-1] > 30) 
				icon = cloudSunIcon;
			
			var tmp = Math.round((parseFloat(val.skin_temp)-273.15)*10)/10;
			
			skinTemp[i] = new Object;
			skinTemp[i].y = tmp;
			skinTemp[i].marker = new Object;
			if(lastIcon % 8 == 0) 
				skinTemp[i].marker.symbol = 'url('+icon+')';
			else 
				skinTemp[i].marker.symbol = 'url(images/transparent.png)';
			lastIcon++;
	
			wind[i] = new Object;
			wind[i].y = vWind;
			wind[i].marker = new Object;
			wind[i].marker.symbol = 'url(SVG2.php?r='+deg+')';
			i++;
		});
		drawChart();
		if (document.getElementById('chartHeader')!=null) {
			document.getElementById('chartHeader').innerHTML='<h1>'+location_name+'</h1>';
		}
		},
		error : function(data, textStatus, jqXHR) {
		},
		dataType : 'json'
	});
	/*$.getJSON('index.php?r=data/getChartData',{'loc':locID, 'limit':limit}, function(data, textStatus, jqXHR){
		var i = 0;
		var lastIcon = 7;
		$.each(data, function(key, val) {		
			xAxis.push(val.datetime_stamp);
			//skinTemp.push(Math.round((parseFloat(val.skin_temp)-273.15)*10)/10);
			soilTemp.push(Math.round((parseFloat(val.soil_temp)-273.15)*10)/10);
			visibility.push(Math.round(parseInt(val.visibility)/10)/100);
			rain.push(Math.round(parseFloat(val.total_col_rain)*100)/100);
			snow.push(Math.round(parseFloat(val.total_col_snow)*100)/100);
			hum.push(Math.round(parseFloat(val.surf_rel_hum)*100)/100);
			cloud.push(Math.round(((parseFloat(val.low_cloud_fra)+parseFloat(val.mid_cloud_fra)+parseFloat(val.high_cloud_fra))/3)*100)/100);
			
			var u = parseFloat(val.u_wind_10m);
			var v = parseFloat(val.v_wind_10m);
			var vWind = Math.round(Math.sqrt(u*u + v*v)*100)/100;
			
			var deg = Math.acos(u/vWind)*(180/Math.PI);
			if(v >= 0) deg = -deg;
		
			var icon = sunIcon;
			if (snow[snow.length-1]  > 1) 
				icon = snowIcon;
			else if (rain[rain.length-1] > 1) 
				icon = rainIcon;
			else if (cloud[cloud.length-1] > 30) 
				icon = cloudSunIcon;
			
			var tmp = Math.round((parseFloat(val.skin_temp)-273.15)*10)/10;
			
			skinTemp[i] = new Object;
			skinTemp[i].y = tmp;
			skinTemp[i].marker = new Object;
			if(lastIcon % 8 == 0) 
				skinTemp[i].marker.symbol = 'url('+icon+')';
			else 
				skinTemp[i].marker.symbol = 'url(images/transparent.png)';
			lastIcon++;
	
			wind[i] = new Object;
			wind[i].y = vWind;
			wind[i].marker = new Object;
			wind[i].marker.symbol = 'url(SVG2.php?r='+deg+')';
			i++;
		});
		drawChart();
		if (document.getElementById('chartHeader')!=null) {
			document.getElementById('chartHeader').innerHTML='<h1>'+location_name+'</h1>';
		}
	});*/
}